package com.hnxxxy.sysmgr.service.impl;

import com.hnxxxy.common.AjaxRtnEntity.AjaxRtnEntity;
import com.hnxxxy.common.tools.TableObj;
import com.hnxxxy.common.tools.TreeObj;
import com.hnxxxy.sysmgr.mapper.RoleMapper;
import com.hnxxxy.sysmgr.mapper.TokenMapper;
import com.hnxxxy.sysmgr.pojo.Role;
import com.hnxxxy.sysmgr.pojo.Token;
import com.hnxxxy.sysmgr.service.RoleService;
import com.hnxxxy.sysmgr.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Json
 * @date 2020-26-8:07
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Autowired
    private TokenMapper tokenMapper;
    /**
     * 查询所有角色
     * @param roleVo
     * @return
     */
    @Override
    public List<Role> queryAllRole(RoleVo roleVo) {
        List<Role> data = this.roleMapper.queryAllRole(roleVo);
        return data;
    }

    /**
     * 添加角色
     * @param roleVo
     */
    @Override
    public Integer addRole(RoleVo roleVo) {

       try {
           roleMapper.insertRole(roleVo);
           return AjaxRtnEntity.success;
       }catch (Exception e){
           e.printStackTrace();
           return AjaxRtnEntity.error;
       }
    }

    /**
     * 修改角色
     * @param roleVo
     */
    @Override
    public Integer updateRole(RoleVo roleVo) {
        try {
            roleMapper.updateRole(roleVo);
            return AjaxRtnEntity.success;
        }catch (Exception e){
            e.printStackTrace();
            return AjaxRtnEntity.error;
        }
    }

    /**
     * 删除角色
     * @param id
     */
    @Override
    public Integer deleteRole(Integer id) {
        
        try {
            //删除sys_role数据
            roleMapper.deleteRole(id);
            // 根据角色id删除sys_staff_role里面的数据
            this.roleMapper.deleteRoleStaffByRid(id);
            return AjaxRtnEntity.success;
        }catch (Exception e){
            e.printStackTrace();
            return AjaxRtnEntity.error;
        }
    }

    /**
     * 批量删除角色
     * @param ids
     */
    @Override
    public Integer batchDeleterole(Integer[] ids) {
        try {
            for (Integer id : ids) {
                deleteRole(id);
            }
            return AjaxRtnEntity.success;
        }catch (Exception e){
            e.printStackTrace();
            return AjaxRtnEntity.error;
        }
    }

    /**
     * 加载角色管理分配菜单的json
     * @param roleid
     * @return
     */
    @Override
    public TableObj initRoleMenuTreeJson(Integer roleid) {
        Token token = new Token();
        //查询所有权限
        List<Token> allToken = tokenMapper.queryAllToken(token);
        //查询当前角色拥有的权限
        List<Token> roleToken = tokenMapper.queryTokenByRoleId(roleid);

        List<TreeObj> data =new  ArrayList<>();

        for (Token m1 : allToken) {
            String checkArr ="0";
            for (Token m2 : roleToken) {
                if (m1.getId().equals(m2.getId())) {
                    checkArr = "1";
                    break;
                }
            }
            Integer id = m1.getId();
            Integer pid;
            if (m1.getMenu_id() == null){
                pid = 0;
            }else {
                pid=m1.getMenu_id();
            }
            String title = m1.getTokenname();
            data.add(new TreeObj(id, pid, title, checkArr));
        }
        return new TableObj(data);

    }

    /**
     * 保存角色和菜单的关系
     * @param roleVo
     */
    @Override
    public Integer saveRoleToken(RoleVo roleVo) {
        Integer   rid = roleVo.getId();
        Integer[] tids= roleVo.getIds();
        //根据rid删除sys_role_token里面所有数据
        this.roleMapper.deleteRoleTokenByRid(rid);
        try {
            //保存角色和菜单的关系
            if (tids != null && tids.length > 0){
                for (Integer tid : tids) {
                    this.roleMapper.insertRoleToken(rid,tid);
                }
            }
            return AjaxRtnEntity.success;
        }catch (Exception e){
            e.printStackTrace();
            return AjaxRtnEntity.error;
        }
    }
}
